package com.test.agent.controller;

import com.test.agent.service.AgentService;
import com.test.common.response.ResponseResult;
import com.test.common.utils.CommandUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/perf/agent")
@ResponseResult
public class AgentController {

    @Resource
    private AgentService agentService;

    @Resource
    private CommandUtils commandUtils;

    @GetMapping("startTask")
    public Object startTask(int reportId) {
        log.info("开始执行任务（报告）：{}", reportId);
        return agentService.startTask(reportId);
    }

    @GetMapping("stopTask")
    public Object stopTask(String reportUuid) {
        return agentService.stopTask(reportUuid);
    }

    @GetMapping("reflushStatus")
    public Object reflushStatus() {
        return agentService.reflushStatus();
    }

    @GetMapping("test")
    public Object test() throws Exception {
        log.info(ClassUtils.getDefaultClassLoader().getResource("").getPath().split("/config")[0]);
        commandUtils.exec("ps -ef | grep /opt/apps/test.jmx");
        return "test";
    }
}
